import 'dart:io';

import 'package:connectivity/connectivity.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';

import 'app_exception.dart';


// 自定义一个socket实例，因为dio原生的实例，message属于是只读的
class MyDioSocketException extends SocketException{

  @override
  late String message;
  MyDioSocketException(message,
   {osError,address, port}) 
   :super(
     message, 
     osError: osError, 
     address: address, 
     port: port
     );
  
}

//错误连接器
class ErrorInterceptor extends Interceptor {
   // 是否有网
  Future<bool> isConnected() async {
    var connectivityResult = await (Connectivity().checkConnectivity());
    return connectivityResult != ConnectivityResult.none;
  }


  @override
  Future <void> onError(DioError err, ErrorInterceptorHandler handler) async {
    if (err.error is SocketException) {
      err.error = MyDioSocketException(err.message, osError: err.error.osError, address: err.error.address, port: err.error.port);
    }
    if (err.type == DioErrorType.other) {
      bool isConnectNetwork = await isConnected();
      if (!isConnectNetwork && err.error is MyDioSocketException) {
        err.error.message = "当前网络不可用，请检查您的网络";
      }
    }
    AppException appException = AppException.create(err);
    // 错误提示
    debugPrint('DioError===: ${appException.toString()}');
    err.error = appException;
    return super.onError(err, handler);
  }
}